package com.abel.main.persistence;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author abel.he
 * @data 2018/8/7 15:43
 */
public class Producer {

    public static void main(String[] args) {
        sendQueueMessage("persistence 数据库持久化完成");
        sendTopicMessage("persistence topic数据库持久化完成");
    }

    /**
     * 发送对列消息
     * @param data 消息内容
     */
    private static void sendQueueMessage(String data) {
        ConnectionFactory factory = null;
        Connection connection = null;
        Session session = null;
        Destination destination = null;
        MessageProducer producer = null;
        Message message = null;

        try {
            factory = new ActiveMQConnectionFactory("hadoop", "hadoop", "tcp://192.168.26.50:61616");
            connection = factory.createConnection();

            session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
            destination = session.createQueue("persistence");
            producer = session.createProducer(destination);
            message = session.createTextMessage(data);
            producer.send(message, DeliveryMode.PERSISTENT, 2, 10 * 1000);
            System.out.println("消息发送成功了");
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            if (producer != null) {
                try {
                    producer.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 发送订阅消息
     * @param data
     */
    private static void sendTopicMessage(String data) {
        ConnectionFactory factory = null;
        Connection connection = null;
        Session session = null;
        Destination destination = null;
        MessageProducer producer = null;
        Message message = null;

        try {
            factory = new ActiveMQConnectionFactory("hadoop", "hadoop", "tcp://192.168.26.50:61616");
            connection = factory.createConnection();

            session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
            destination = session.createTopic("persistence");
            producer = session.createProducer(destination);
            message = session.createTextMessage(data);
            producer.send(message, DeliveryMode.PERSISTENT, 2, 10 * 1000);
            System.out.println("消息发送成功了");
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            if (producer != null) {
                try {
                    producer.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    }



}
